<%-- 
    Document   : messages
    Created on : Mar 4, 2014, 10:36:09 PM
    Author     : Jordan,Theodoro
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<c:if test="${sessionScope.username == NULL}">
    <c:redirect url="http://webtek.org/Bonix/php/public/index.php"/>
</c:if>
<sql:setDataSource user="root" password="" driver="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost/bonix"/>

<c:set scope="request" var="title" value="Message"/>
<c:set var="startFromRecNo" value="0"/>
<c:if test="${param.start != null}">
    <c:set var="startFromRecNo" value="${param.start}"/>
</c:if>

<sql:query var="getMessages">
    Select distinct entry_id,sender,receiver,substring(msg,1,10) as "message",DATE(sent_date) as "date",status,TIME(sent_date) as "time" from messages 
    where receiver = ?
    order by sent_date DESC
    <sql:param value="${sessionScope.username}"/>
</sql:query>

<sql:query var="senders">
    Select distinct sender from messages
    where receiver = ?
    order by sent_date asc
    <sql:param value="${sessionScope.username}"/>
</sql:query>

<!DOCTYPE html>
<html>
    <jsp:include page="head.jsp"/>

    <body>
        <jsp:include page="banner.jsp"/>


        <div class="row">
            <div class="container_header">
                <div class="content row">

                    <div class="containerService">

                        <div class="panel panel-info">

                            <div class="panel-heading">
                                <ul class="navClient navClient-pills">

                                    <c:url var="messagesURL" value="messages.jsp" />
                                    <li><a href="${messagesURL}"> Message Inbox </a></li>

                                    <c:url var="composeURL" value="compose.jsp" />
                                    <li><a href="${composeURL}"> Compose Message </a></li>

                                    <c:url var="sentItemsURL" value="sentItems.jsp" />
                                    <li><a href="${sentItemsURL}"> Sent Items </a></li>
                                </ul>
                            </div>

                            <div class="panel-body">
                                <br><hr><br>
                                <table class="table table-hover table-condensed">
                                    <c:if test="${startFromRecNo > 0}">
                                        <c:url var="prevURL" value="messages.jsp">
                                            <c:param name="start" value="${startFromRecNo - 8}"></c:param>
                                        </c:url>
                                        <a href="${prevURL}">Previous</a>
                                    </c:if>

                                    <c:if test="${getMessages.limitedByMaxRows}">
                                        <c:url var="nextURL" value="messages.jsp">
                                            <c:param name="start" value="${startFromRecNo + 8}"></c:param>
                                        </c:url>
                                        <a href="${nextURL}">Next</a>
                                    </c:if>

                                    <br>
                                    <thead>  
                                        <tr>  
                                            <th style="width:150px">STATUS: </th>
                                            <th style="width:150px">MESSAGE FROM:</th>  
                                            <th style="width:150px">DATE:</th>
                                            <th style="width:150px">TIME:</th>
                                            <th>MESSAGE:</th>   
                                        </tr>  
                                    </thead> 
                                    <tbody>
                                        <c:forEach var="sender" items="${senders.rows}">
                                            <tr class="warning">
                                                <c:set var="count" value="0" />
                                                <c:forEach var="message" step="1" items="${getMessages.rows}">

                                                    <c:if test="${(message.sender == sender.sender) && count=='0'}">
                                                        <c:url var="messageItemURL" value="viewConversation.jsp">
                                                            <c:param name="sender" value="${message.sender}" />
                                                        </c:url>
                                                        <td><a href="${messageItemURL}"/>${message.status}</td>
                                                        <td><a href="${messageItemURL}"/>${message.sender}</td>  
                                                        <td>${message.date}</td>
                                                        <td>${message.time}</td>  
                                                        <td>${message.message}</td>
                                                        <c:set var="count" value="1" />
                                                    </c:if>
                                                </c:forEach>      
                                            </tr>
                                        </c:forEach>    

                                    </tbody>
                                </table>
                            </div><!-- END panel-body -->

                        </div><!-- END panel-info -->
                    </div><!-- END containerService -->

                </div><!-- END CONTENT CONTENT ROW -->
            </div><!-- END CONTAINER -->
        </div><!-- END ROW -->

        <jsp:include page="footer.jsp"/>

    </body>
</html>
